package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.constant.enums.InvAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.DcpgCountyAuditCreateDTO;
import com.engine4cloud.tdf.supervision.dto.create.DcpgInvestigationCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.DcpgInvCrimeNameResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.DcpgInvestigationResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxFileResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.DcpgInvestigationUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.DcpgInvestigationEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Mapper
public interface DcpgInvestigationConvert {
    DcpgInvestigationConvert INSTANCE = Mappers.getMapper(DcpgInvestigationConvert.class);

    DcpgInvestigationResultDTO convert(DcpgInvestigationEntity s);

    DcpgInvestigationEntity convert(DcpgInvestigationResultDTO s);

    DcpgInvestigationEntity createTo(DcpgInvestigationCreateDTO s);

    DcpgInvestigationEntity updateTo(DcpgInvestigationUpdateDTO s);

    DcpgInvestigationEntity convertTo(DcpgInvestigationResultDTO s);

    default DcpgInvestigationEntity create(DcpgInvestigationCreateDTO s, Long deptId, Long tenantId, InvAuditActionStatusEnum action) {
        DcpgInvestigationEntity entity = createTo(s);
        //设置调查评估编号
        entity.setDcpgbh(CodeUtils.entrustCode());
        //设置部门
        entity.setDeptId(deptId);
        //设置租户id
        entity.setTenantId(tenantId);
        //设置状态
        entity.setPreAction(action);
        //设置审核状态
        additionalArea(entity, s.getResidenceAreaList());
        return entity;
    }

    default DcpgInvestigationEntity update(DcpgInvestigationUpdateDTO s) {
        DcpgInvestigationEntity entity = updateTo(s);
        additionalArea(entity, s.getResidenceAreaList());
        return entity;
    }

    default DcpgInvestigationEntity convert(DcpgInvestigationResultDTO s, DcpgCountyAuditCreateDTO dto) {
        DcpgInvestigationEntity entity = convertTo(s);
        entity.setDcjgsm(dto.getShsm());
        return entity;
    }

    default void renderAreaList(DcpgInvestigationResultDTO s) {
        if (Objects.isNull(s)) {
            return;
        }

        List<String> residenceAreaList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(s.getBgrjzdszs()), () -> residenceAreaList.add(s.getBgrjzdszs()));
        Actions.apply(StringUtils.isNotEmpty(s.getBgrjzdszds()), () -> residenceAreaList.add(s.getBgrjzdszds()));
        Actions.apply(StringUtils.isNotEmpty(s.getBgrjzdszxq()), () -> residenceAreaList.add(s.getBgrjzdszxq()));
        Actions.apply(StringUtils.isNotEmpty(s.getBgrjzd()), () -> residenceAreaList.add(s.getBgrjzd()));

        s.setResidenceAreaList(residenceAreaList);
    }

    default void additionalArea(DcpgInvestigationEntity s, List<String> residenceAreaList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(residenceAreaList)) {
            return;
        }
        s.setBgrjzdszs(residenceAreaList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        s.setBgrjzdszds(residenceAreaList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));

        if (residenceAreaList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH) {
            s.setBgrjzdszxq(residenceAreaList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX));
        } else {
            s.setBgrjzdszxq(null);
        }

        if (residenceAreaList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH) {
            s.setBgrjzd(residenceAreaList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX));
        } else {
            s.setBgrjzd(null);
        }
    }

    default DcpgInvestigationResultDTO additionalInfo(DcpgInvestigationResultDTO inv, List<DcpgInvCrimeNameResultDTO> crimeNameList, List<JzdxFileResultDTO> fileList) {
        if (Objects.isNull(inv) || CollectionUtils.isEmpty(crimeNameList) || CollectionUtils.isEmpty(fileList)) {
            return inv;
        }

        Map<String, List<DcpgInvCrimeNameResultDTO>> crimeInfoListMap = crimeNameList.stream()
                .collect(Collectors.groupingBy(DcpgInvCrimeNameResultDTO::getDcpgbh));

        if (crimeInfoListMap.containsKey(inv.getDcpgbh())) {
            inv.setCrimeNameList(crimeInfoListMap.get(inv.getDcpgbh()));
        }

        Map<String, List<JzdxFileResultDTO>> fileListMap = fileList.stream()
                .collect(Collectors.groupingBy(JzdxFileResultDTO::getDcpgbh));

        if (fileListMap.containsKey(inv.getDcpgbh())) {
            inv.setInvFileList(fileListMap.get(inv.getDcpgbh()));
        }

        return inv;
    }

}
